package com.cinyi.ormx.android.context;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Date;

import android.database.Cursor;
import android.util.Log;

public class LogHelper {
    private static final String TAG = "LogHelper_vzone";
    
    /**
     * 支持将log写入SDCARD文件
     */
    private static boolean ENABLE_LOG_FILE = true; 
        
    public static final String SDCARD_LOG_FILE = "log.log" ;

	private static final String APP_LOG_PATH = "/sdcard/ormx/logs/";

    /**
     * 是否开启log
     */
    private static final boolean ENNABLE_DEBUG = true ;
    private static final boolean ENABLE_ERROR = true ;
    private static final boolean ENABLE_PRINT = false ;
    
    public static final boolean ENABLE_ERROR_TOAST = false ;
    
    public static void d(String msg){
    	d(TAG,msg);
    }
    
	public static void d(String TAG, String msg) {
		if (!ENNABLE_DEBUG)
			return;
		
		msg = format("", msg);
		Log.d(TAG, msg ); 
		
		if (ENABLE_LOG_FILE) {
			log(TAG, msg);
		} 
	}
    
    private static String format(String tag, String msg) {
        return " - " + msg;
    }
    
    private static String formate(String tag, String msg) {
        return " *** " + msg;
    }
    
    public static void e(String TAG, String msg) {
		if (!ENABLE_ERROR)
			return;
		msg = formate("", msg);
		Log.e(TAG, msg); 
		
		if (ENABLE_LOG_FILE) {
			log(TAG, msg);
		} 
	}
    
	public static void e(String tag, String msg, Exception e) {
		msg = formate("", msg + e.getLocalizedMessage() );
		Log.e(tag, msg); 
		
		if (ENABLE_LOG_FILE) {
			log(tag, msg);
		} 
	}

	 public static void i(String TAG, String msg) {
		if (!ENABLE_ERROR)
			return;
		msg = formate("", msg);
		Log.i(TAG, msg); 
		
		if (ENABLE_LOG_FILE) {
			log(TAG, msg);
		} 
	}
    
	public static void i(String tag, String msg, Exception e) {
		msg = formate("", msg + e.getLocalizedMessage() );
		Log.i(tag, msg); 
		
		if (ENABLE_LOG_FILE) {
			log(tag, msg);
		} 
	}
	
    public static void ex(String TAG,Exception ex) {
		if (!ENABLE_ERROR)
			return;
		StringWriter sw = new StringWriter();
        ex.printStackTrace(new PrintWriter(sw, true));
        String msg = sw.toString();
		Log.e(TAG, msg); 
		
		if (ENABLE_LOG_FILE) {
			log(TAG, msg);
		} 
	}
    
    /**
     * Log on sdcard
     * @param tag
     * @param msg
     */
    private static void log(String tag,String msg){
    	if(android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)){
    		java.io.File dir =new java.io.File(APP_LOG_PATH);
    		if(!dir.exists()){
    			dir.mkdirs();
    		}
    		FileOutputStream outputStream = null;
    		try{
	    		 outputStream = new FileOutputStream(new java.io.File(APP_LOG_PATH ,SDCARD_LOG_FILE),true);
	    		 byte[] log = new StringBuffer().append( new Date().toLocaleString()).append("[").append(tag).append("]")
	    				 .append(" : ").append(msg).append("\r\n").toString().getBytes("utf-8");
	             outputStream.write(log);
	             outputStream.flush();
	             outputStream.close();
    		}catch (IOException e) {
    			ENABLE_LOG_FILE = false ;
    			Log.d(TAG,"write log error,log file can't be write!\r\n" + e.getMessage() );
				e.printStackTrace();
			}finally{
				try {
					if( outputStream != null){					
						outputStream.close();					
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
    	}
    }

    /**
     * 打印对象
     * @param obj
     */
    public static void p(Object obj){
    	if(!ENABLE_PRINT){
    		return ;
    	}
    	
    	if(obj == null){
			d("print object is null !") ;
			return ;
		}

		if(obj instanceof Cursor){
			_print((Cursor)obj);
		}
	}

	/**
	 * 查看
	 * @param obj
	 */
	private static void _print(Cursor cursor) {
		int columnCount = cursor.getColumnCount() ;
		
		if(cursor!=null && cursor.moveToFirst()){
			do{
				 
			}while(cursor.moveToNext());
			cursor.close();
		}
		
	}

}
